package com.cryptape.cita_wallet.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.Toast;

import com.alm.token.R;
import com.cryptape.cita_wallet.activity.transfer.TransferActivity;
import com.cryptape.cita_wallet.adapter.CloudMinerAdapter;
import com.cryptape.cita_wallet.bean.CloudMinerBean;
import com.cryptape.cita_wallet.constant.ConstantUtil;
import com.cryptape.cita_wallet.item.Token;
import com.cryptape.cita_wallet.service.http.TokenService;
import com.cryptape.cita_wallet.util.NumberUtil;
import com.cryptape.cita_wallet.view.SpacesItemDecoration;
import com.cryptape.cita_wallet.view.TitleBar;

import org.web3j.abi.datatypes.DynamicArray;
import org.web3j.abi.datatypes.Type;
import org.web3j.abi.datatypes.generated.Uint256;

import java.util.ArrayList;
import java.util.List;

import rx.Subscriber;

/**
 * Created by dengming on 23/2/20.
 */
public class CloudMinerActivity extends NBaseActivity {

    private RecyclerView recyclerView;
    private CloudMinerAdapter minerAdapter;
    private TitleBar mTitle;
    private List<CloudMinerBean> minerBeans = new ArrayList<>();

    @Override
    protected int getContentLayout() {
        return R.layout.activity_cloud_miner;
    }

    @Override
    protected void initView() {
        recyclerView = findViewById(R.id.clound_miner_recycler);
        mTitle = findViewById(R.id.title_bar);
        mTitle.setOnRightClickListener(new TitleBar.OnRightClickListener() {
            @Override
            public void onRightClick() {
                startActivity(new Intent(CloudMinerActivity.this,MyMinerActivity.class));
            }
        });
        mTitle.mTitleBarRightDefaultView.setCompoundDrawables(null,null,null,null);
    }

    @Override
    protected void initData() {
        initMinerData();
    }

    @Override
    protected void initAction() {

    }

    private void initMinerData() {
        initAdapter();
        showProgressBar();
        TokenService.minerDetail().subscribe(new Subscriber<List<Type>>() {
            @Override
            public void onCompleted() {
                Log.e("MainActivity","onCompleted");
                dismissProgressBar();
            }

            @Override
            public void onError(Throwable e) {
                dismissProgressBar();
                Toast.makeText(CloudMinerActivity.this, getString(R.string.network_error), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNext(List<Type> datas) {
                dismissProgressBar();
                Log.e("MainActivity","onNext");
                if (datas != null && datas.size() >= 3) {
                   DynamicArray<Uint256> value0 = (DynamicArray)(datas.get(0));
                   if(value0 == null){
                       return;
                   }
                   List<Uint256> prices = value0.getValue();
                   if(prices == null || prices.size() == 0){
                       return;
                   }
                    DynamicArray<Uint256> value1 = (DynamicArray)(datas.get(1));
                    if(value1 == null){
                        return;
                    }
                    List<Uint256> totals = value1.getValue();
                    if(totals == null || totals.size() == 0){
                        return;
                    }
                    minerBeans.clear();
                   int length = prices.size();
                   for(int index = 0;index<length;index++)
                   {
                       CloudMinerBean bean = new CloudMinerBean();
                       bean.setType(index);
                       bean.setPrice(""+NumberUtil.getEthFromWei(prices.get(index).getValue()));
                       bean.setLeftAmount(totals.get(index).getValue().toString());
                       minerBeans.add(bean);
                   }
                   minerAdapter.notifyDataSetChanged();
                }
            }
        });
    }


    private void initAdapter() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mActivity);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);

//        minerBeans.add(new CloudMinerBean());
        recyclerView.addItemDecoration(new SpacesItemDecoration(15));
        minerAdapter = new CloudMinerAdapter(this,minerBeans);
        recyclerView.setAdapter(minerAdapter);

        minerAdapter.setOnItemClickListener((view, position) -> {
            Token token = new Token(ConstantUtil.ETH_MAINNET, ConstantUtil.HMIO_TOKEN_SYMBOL, ConstantUtil.ETHEREUM_MAIN_ID);
            Intent intent = new Intent(CloudMinerActivity.this, MinerPayActivity.class);
            token.setContractAddress(ConstantUtil.getHToken());// "0xb2054aDd212F19581aEB195FDa7644471a996134";
            token.decimals = 18;
            intent.putExtra(TransferActivity.EXTRA_TOKEN, token);
            intent.putExtra(TransferActivity.EXTRA_ADDRESS, ConstantUtil.getMinerSellContract());
            intent.putExtra(MinerPayActivity.class.getSimpleName(),minerBeans.get(position));
            startActivity(intent);
        });
    }
}
